package org.mxp.controller;

import org.mxp.service.TableMessageService;
import org.mxp.utils.ChatMessageUtil;
import org.mxp.wobsocket.WebSocketServer;
import org.mxp.utils.TableMessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "init", produces = "text/html;charset=utf-8")
public class InitMessageController {
    @Autowired
    private TableMessageUtil tableMessageUtil;
    @Autowired
    private ChatMessageUtil chatMessageUtil;
    @Autowired
    private TableMessageService tableMessageService;

    @RequestMapping("table")
    public String initTableMessage() {
        return tableMessageUtil.getAllCell();
    }

    @RequestMapping("chat")
    public String initChatMessage() {
        return chatMessageUtil.getChatAll();
    }

    @RequestMapping("table/clear")
    public String clearTableMessage(String pwd) {
        if ("1a2b3c".equals(pwd)) {
            tableMessageUtil.clear();
            WebSocketServer.clear();
            return "OK";
        }
        return "NO";
    }

    @ResponseBody
    @RequestMapping("table/list")
    public List<Map<String, Object>> tableList() {
        List<Map<String, Object>> list = tableMessageService.list();
        return list;
    }

    @RequestMapping("chat/changeSpray")
    public String tableList(String userId) {
        chatMessageUtil.changeSpray(userId);
        return "ok";
    }
}
